package com.ujiuye.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.pojo.User;
import com.ujiuye.service.HomeworkService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 导包
 *  jdbc 1   c3p0 2 dbutils 1   jstl 2   jackson 3
 *  导入配置文件   c3p0
 */

@WebServlet("/homeworkServlet")
public class HomeworkServlet extends BaseServlet {
    private HomeworkService hs = new HomeworkService();
    /**
     * 验证账号唯一
     * @param req
     * @param resp
     */
    public void validUname(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uname = req.getParameter("uname");
        //传递给service
        //  b true 查得到   已存在    false  可以注册
        boolean b = hs.validUname(uname);
        resp.getWriter().print(b);
    }

    /**
     * 模糊查询
     * @param req
     * @param resp
     */
    public void dimQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uname = req.getParameter("uname");
        //调用service
        List<User> lists = hs.dimQuery(uname);
        //报错 null  查不到 list 空    查得到  有东西

        String json = new ObjectMapper().writeValueAsString(lists);
        resp.getWriter().print(json);



    }
}
